[Android] Show default selection color for custom listview

Posted by Diego on Stack Overflow See other posts from Stack Overflow or by Diego
Published on 2010-09-04T16:10:28Z Indexed on 2011/01/10 19:53 UTC
Read the original article Hit count: 209

Filed under:
|

Hello, I have a listview with a custom BaseAdapter. Each row of the listview has a TextView and a CheckBox.

The problem is when I click (or touch) any row, the textview foreground becomes gray, instead of the default behavior (background -> green, textview foreground -> white).

Here is the code:

row.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                style="@style/layout">


    <TextView android:id="@+id/main_lv_item_textView" 
              style="@style/textViewBig"
              android:layout_alignParentLeft="true"/>


    <CheckBox android:id="@+id/main_lv_item_checkBox" 
              style="@style/checkBox"
              android:layout_width="wrap_content"
              android:layout_alignParentRight="true"/>

</RelativeLayout>

Custom Adapter:

public class CustomAdapter extends BaseAdapter {
        private List<Profile> profiles;
        private LayoutInflater inflater;
        private TextView tvName;
        private CheckBox cbEnabled;

        public CustomAdapter(List<Profile> profiles) {
            this.profiles = profiles;
            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        public int getCount() {
            return profiles.size();
        }

        public Object getItem(int position) {
            return profiles.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(final int position, View convertView, ViewGroup parent) {
            View row = inflater.inflate(R.layout.main_lv_item, null);

            final Profile profile = profiles.get(position);
            tvName = (TextView) row.findViewById(R.id.main_lv_item_textView);
            registerForContextMenu(tvName);
            cbEnabled = (CheckBox) row.findViewById(R.id.main_lv_item_checkBox);
            tvName.setText(profile.getName());
            if (profile.isEnabled()) {
                cbEnabled.setChecked(true);
            }

            tvName.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    Bundle bundle = new Bundle();
                    bundle.putString(PROFILE_NAME_KEY, profile.getName());
                    Intent intent = new Intent(context, GuiProfile.class);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });

            tvName.setOnLongClickListener(new OnLongClickListener() {
                public boolean onLongClick(View v) {
                    selectedProfileName = ((TextView) v).getText().toString();
                    return false;
                }
            });

            cbEnabled.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (!profile.isEnabled()) {
                        for (Profile profile : profiles) {
                            if (profile.isEnabled()) {
                                profile.setEnabled(false);
                                Database.getInstance().storeProfile(profile);
                            }
                        }
                    }

                    profile.setEnabled(isChecked);
                    Database.getInstance().storeProfile(profile);
                    updateListView();
                }
            });

            return row;
        }
    }

Any help would be appreciated.

© Stack Overflow or respective owner

Related posts about android

Related posts about listview